package com.lagou.mrsort;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class SortDriver {

    public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException {

        //定义输入、输出路径
        args = new String[]{"src/input","src/output"};

        //获取配置文件对象，获取job对象实例
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        //指定程序jar的本地路径
        job.setJarByClass(SortDriver.class);

        //指定Mapper/Reducer类
        job.setMapperClass(SortMapper.class);
        job.setReducerClass(SortReducer.class);

        //指定Mapper输出的kv数据类型
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(NullWritable.class);

        //指定最终输出的kv数据类型
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);

        //指定job处理的原始数据路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        //指定job输出结果路径
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        //提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0:1);
    }
}
